﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using WmsService.Reports;
using WmsService.Util;

namespace WmsService
{
    public partial class Service
    {
        //private List<ReportParameter> kk = null;

        #region 报表
        public DataSet GetDataSet(string reportID, List<ReportParameter> reportParameters)
        {
            var sql = "select * from dbo.{0} where 1=1 {1} ";
            //var ctx = Factory.ContextFactory.Create();
            var reportWrap = GetReportWrap(reportID);
            using (var ds = new DataSet(reportWrap.FReport.ReportID))
            {
                foreach (var reportDtl in reportWrap.FReportDetailList)
                {
                    if (reportDtl != null)
                    {
                        ReportDetail dtl = reportDtl;
                        List<ReportParameter> parameters =
                            reportParameters.FindAll(
                                o => String.Equals(o.DbTableName, dtl.DbTableName, StringComparison.OrdinalIgnoreCase)).
                                ToList();
                        if (parameters.Count() > 0)
                        {        
                            var dt = DataAccess.DataQuerier.GetDataTable(String.Format(sql, reportDtl.DbTableName, parameters[0].Condition));
                            dt.TableName = reportDtl.DsTableName;
                            //复制，包括结构和数据
                            ds.Tables.Add(dt.Copy());
                        }
                        else
                        {
                            var dt = DataAccess.DataQuerier.GetDataTable(String.Format(sql, reportDtl.DbTableName, " and 1=2"));
                            dt.TableName = reportDtl.DsTableName;
                            //克隆一个schema而已，没有数据
                            ds.Tables.Add(dt.Clone());
                        }
                    }
                }

                //处理切片，可能是增加字段，或者改变dataset数据等等 
                //每一个reportWrap都要经过处理才能返回给客户端。
                reportWrap.HandleAopDataSet(ds);

                ds.AcceptChanges();
                return ds;
            }
        }

        public List<Report> GetReports()
        {
            var ctx = Factory.ContextFactory.Create();
            var query = from o in ctx.Report
                        select o;
            return query.ToList();
        }

        public void SaveReports(List<Report> reports)
        {
            throw new NotImplementedException();
        }

        public List<Report> GetReportsByCondition(string condition)
        {
            try
            {
                var ctx = Factory.ContextFactory.Create();
                return ctx.ExecuteStoreQuery<Report>("select * from dbo.Report  where 1=1 " + condition).ToList();
            }
            catch (Exception ex)
            {
                Exceptions.ExceptionHandler.ThrowException(ex);
                return null;
            }
        }

        public List<ReportDetail> GetReportDetails()
        {
            var ctx = Factory.ContextFactory.Create();
            var query = from o in ctx.ReportDetail
                        select o;
            return query.ToList();
        }

        public void SaveReportDetails(List<ReportDetail> reportDetails)
        {
            throw new NotImplementedException();
        }

        public List<ReportDetail> GetReportDetailsByCondition(string condition)
        {
            try
            {
                var ctx = Factory.ContextFactory.Create();
                return ctx.ExecuteStoreQuery<ReportDetail>("select * from dbo.ReportDetail  where 1=1 " + condition).ToList();
            }
            catch (Exception ex)
            {
                Exceptions.ExceptionHandler.ThrowException(ex);
                return null;
            }
        }

        public ReportWrap GetReportWrap(string reportID)
        {
            var wrap = new ReportWrap();
            var reports = GetReportsByCondition(String.Format(" and ReportID ='{0}'", reportID));
            if (reports.Count == 0)
            {
                Exceptions.ExceptionHandler.ThrowException(new Exception("不存在报表ID： " + reportID));
            }
            wrap.FReport = reports[0];
            wrap.FReportDetailList = GetReportDetailsByCondition(String.Format(" and ReportID ='{0}'", reportID));
            return wrap;
        }
        #endregion
    }
}
